package com.huitone.gddw.controller.monitoring.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.ItemDef;
import com.huitone.gddw.dao.entity.ObjItem;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.monitoring.base.ItemDefDTO;
import com.huitone.gddw.service.monitoring.base.IItemDefService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 对象子项
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="itemDef")
public class ItemDefController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(ItemDefController.class);
	
	@Autowired
	private IItemDefService itemDefService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<ItemDefDTO> query(QueryPage<ItemDef> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询子对象列表";
		QueryPage<ItemDefDTO> result = null;
		try {
			page.setParams(getParams(request));
			result = itemDefService.selectPage(page);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return result;
		} catch (Exception e) {
			LOGGER.error("查询子对象信息出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return result;
		}
	}
	
	/**
	 * 获取参数信息
	 * @param request
	 * @return Map<String, Object>
	 */
	private Map<String, Object> getParams(HttpServletRequest request) {
		String objId = request.getParameter("objId");
		String objType = request.getParameter("objType");
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("objId", objId);
		params.put("objType", objType);
		return params;
	}
	
	/**
	 * 获取指定对象下的所有子项信息
	 * @param objId 对象ID
	 * @return List<ObjItem>
	 */
	@ResponseBody
	@RequestMapping(value="getByObjId")
	public List<ObjItem> getByObjId(String objId) {
		try {
			return itemDefService.getByObjId(objId);
		} catch (Exception e) {
			LOGGER.error("获取指定对象下的所有子项信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 准备修改页面的信息
	 * @param itemId 子项ID
	 * @return ItemDefDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public ItemDefDTO toUpdate(String itemId) {
		try {
			return itemDefService.findOne(itemId);
		} catch (Exception e) {
			LOGGER.error("监控配置-子对象管理-准备修改页面信息时出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param entity ItemDef
	 * @param request
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(ItemDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加子对象信息";
		try {
			itemDefService.save(entity);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存子对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
	/**
	 * 修改
	 * @param entity ItemDef
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(ItemDef entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改子对象信息";
		try {
			ItemDef itemDef = itemDefService.getById(entity.getItemId());
			itemDef.setItemName(entity.getItemName());
			itemDef.setObjId(entity.getObjId());
			itemDef.setItemType(entity.getItemType());
			itemDef.setApplications(entity.getApplications());
			itemDef.setRemark(entity.getRemark());
			itemDefService.update(itemDef);
			
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "修改成功！");
		} catch (Exception e) {
			LOGGER.error("修改子对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "修改失败！");
		}
	}
	
	/**
	 * 删除
	 * @param itemId 子项ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String itemId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除子对象信息";
		try {
			itemDefService.delete(itemId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除子对象信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
}
